package com.czk.model.decoratot.runnabledecorator;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author chenzhangkun
 * @ClassName ThreadDecoratorPool
 * @description: TODO
 * @date 2025年08月08日
 */
public class ThreadDecoratorPool {
    static ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public ThreadDecoratorPool() {
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(200);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("czk-");
        // runnable装饰器，会将runnable进行包装
        executor.setTaskDecorator(runnable -> () -> {
            // 为每个线程设置一个上下文变量
            threadLocal.set(Thread.currentThread().getName());
            runnable.run();
        });
        executor.initialize();

    }
    public void test(){
        for (int i = 0; i < 8; i++) {
            executor.execute(()->{
                System.out.println(threadLocal.get());
            });
        }
    }

    public static void main(String[] args) {
        ThreadDecoratorPool threadDecoratorPool = new ThreadDecoratorPool();
        threadDecoratorPool.test();
    }


}
